package com.woniu.defecttracking.controller.bugs;


import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.FindBugDto;
import com.woniu.defecttracking.dto.FindPriortyDto;
import com.woniu.defecttracking.dto.FindSavarityDto;
import com.woniu.defecttracking.entity.Savarity;
import com.woniu.defecttracking.exception.DefectTrackingException;
import com.woniu.defecttracking.service.SavarityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何家欣
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/savarity")
public class SavarityController {
    @Autowired
    private SavarityService savarityService;
    @PostMapping("/findAllSavarity")
    public Result findAllSavarity(){
        //返回全部状态的
        FindSavarityDto savarity = savarityService.findSavarity();
        if (savarity==null){
            return Result.getInstance(savarity).setMessage("查询失败").setCode(500).setData(savarity);

        }
        return Result.getInstance(savarity).setMessage("查询成功").setCode(200).setData(savarity);
    }
    @PostMapping("/findSavarity")
    public Result findOpenSavarity(){
        //只返回开启状态的
        FindSavarityDto savarity = savarityService.findOpenSavarity();
        if (savarity==null){
            return Result.getInstance(savarity).setMessage("查询失败").setCode(500).setData(savarity);

        }
        return Result.getInstance(savarity).setMessage("查询成功").setCode(200).setData(savarity);
    }

    @PostMapping("/addSavarity")
    public Result addSavarity(@RequestBody FindSavarityDto addSavarityDto){
        boolean b = savarityService.addSavarity(addSavarityDto);
        if (b==false){
            return Result.getInstance(b).setMessage("添加失败").setCode(500).setData(b);

        }
        return Result.getInstance(b).setMessage("添加成功").setCode(200).setData(b);
    }

    @PostMapping("/updateSavarity")
    public Result updateSavarity(@RequestBody FindSavarityDto updateSavarityDto){
        boolean b = savarityService.updateSavarity(updateSavarityDto);
        if (b==false){
            return Result.getInstance(false).setMessage("修改失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("修改成功").setCode(200);
}
    @PostMapping("/deleteSavarity")
    public Result deleteBugInfo(@RequestBody List<Integer> ids) throws DefectTrackingException {
        boolean b = savarityService.deleteSavarity(ids);
        if (b==false){
            return Result.getInstance(false).setMessage("删除失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("删除成功").setCode(200);
    }

    //开启
    @GetMapping("/updateStatusUp/{id}")
    public Result updateStateUp(@PathVariable Integer id){
        boolean b = savarityService.updateStatusUp(id);
        if (b==false){
            return Result.getInstance(false).setMessage("开启失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("开启成功").setCode(200);
    }
    //关闭
    @GetMapping("/updateStatusDown/{id}")
    public Result updateStateDown(@PathVariable Integer id){
        boolean b = savarityService.updateStatusDown(id);
        if (b==false){
            return Result.getInstance(false).setMessage("关闭失败").setCode(500);

        }
        return Result.getInstance(true).setMessage("关闭成功").setCode(200);
    }
}

